package org.siam.impl.data {
	
	import flash.utils.Dictionary;
	
	import mx.core.IFactory;
	
	import org.siam.core.data.IDataContext;
	import org.siam.core.data.IDataManager;
	
	public class DataManager implements IDataManager {
		
		public static const DEFAULT_ID:String = "default"; 
		
		private var _registry:Dictionary;
		private var _default:IDataContext;
		
		public function DataManager() {
			_registry = new Dictionary();
			_default = null;
		}
		
		public function has(id:String):Boolean {
			return _registry.hasOwnProperty(id);
		}
		
		protected function findContext(id:String):IDataContext {
			return has(id) ? _registry[id] : _default;
		}
		
		protected function createContext(factory:IFactory, metadata:Object):IDataContext {
			return null;
		}
		
		public function register(id:String, factory:IFactory, metadata:Object):void {
			if (!has(id)) {
				var context:IDataContext = createContext(factory, metadata); 
				_registry[id] = context;
				if (id == DEFAULT_ID) {	
					_default = context;
				}
			}
		}
	}
}